<?php

/**
 * This is the model class for table "{{emails}}".
 *
 * The followings are the available columns in table '{{emails}}':
 * @property integer $zes_id
 * @property string $zes_title
 * @property integer $zes_type
 * @property string $zes_content
 * @property string $zm_ids
 * @property string $zml_tomail
 * @property integer $zml_totle
 * @property integer $zml_status
 * @property integer $zml_success
 * @property integer $zml_error
 * @property string $zml_errormail
 * @property integer $inputtime
 */
class Emails extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Emails the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{emails}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zes_title, zes_type, zes_content', 'required'),
			array('zes_type, zml_totle, zml_status, zml_success, zml_error, inputtime', 'numerical', 'integerOnly'=>true),
			array('zes_title', 'length', 'max'=>100),
			array('zm_ids', 'length', 'max'=>255),
			array('zml_tomail', 'length', 'max'=>1000),
			array('zml_errormail', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zes_id, zes_title, zes_type, zes_content, zm_ids, zml_tomail, zml_totle, zml_status, zml_success, zml_error, zml_errormail, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zes_id' => '邮件',
			'zes_title' => '标题',
			'zes_type' => '类型',
			'zes_content' => '内容',
			'zm_ids' => '会员ID',
			'zml_tomail' => '收件人',
			'zml_totle' => '邮件总数',
			'zml_status' => '状态',
			'zml_success' => '成功数量',
			'zml_error' => '失败数量',
			'zml_errormail' => '失败邮件地址',
			'inputtime' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zes_id',$this->zes_id);
		$criteria->compare('zes_title',$this->zes_title,true);
		$criteria->compare('zes_type',$this->zes_type);
		$criteria->compare('zes_content',$this->zes_content,true);
		$criteria->compare('zm_ids',$this->zm_ids,true);
		$criteria->compare('zml_tomail',$this->zml_tomail,true);
		$criteria->compare('zml_totle',$this->zml_totle);
		$criteria->compare('zml_status',$this->zml_status);
		$criteria->compare('zml_success',$this->zml_success);
		$criteria->compare('zml_error',$this->zml_error);
		$criteria->compare('zml_errormail',$this->zml_errormail,true);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = "inputtime DESC";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取类型
	 */
	public function getType($item=null){
		if($item == null){
			return array(
					'1'=>'全部订阅人',
			//		'2'=>'指定会员',
					'3'=>'填写邮件地址',
				  );
		}else{
			$data = $this->getType();
			if(array_key_exists($item,$data))
				return $data[$item];
		}
	}
	
	/**
	 * 获取状态
	 */
	public function getStatus($item=null){
		if($item == null){
			return array(
					'1'=>'队列中',
					'2'=>'完成',
					'3'=>'草稿',
			);
		}else{
			$data = $this->getStatus();
			if(array_key_exists($item,$data))
				return $data[$item];
		}
	}
	
	/**
	 * 保存前的操作
	 */
	public function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
		}
		return true;
	}
}